package com.pipe.servicepipe;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @ClassName Demo
 * @Author HETAO
 * @Date 2020/7/12 10:40
 */
public class Demo {

    @Test
    public void test() {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode("jwdlh123");
        System.out.println(encode);
    }

    @Test
    public void run() {

        // 、创建代码生成器

        AutoGenerator mpg = new AutoGenerator();


        // 、全局配置

        GlobalConfig gc = new GlobalConfig();

        String projectPath = System.getProperty("user.dir");

        System.out.println(projectPath);

        gc.setOutputDir(projectPath + "/src/main/java");

        gc.setAuthor("hetao");

        gc.setOpen(false); //生成后是否打开资源管理器

        gc.setFileOverride(false); //重新生成时文件是否覆盖

        /*

         * mp生成service层代码，默认接口名称第一个字母有 I

         * UcenterService

         * */

        gc.setServiceName("%sService"); //去掉Service接口的首字母I

        gc.setIdType(IdType.ID_WORKER); //主键策略

        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型

        gc.setSwagger2(true);//开启Swagger模式


        mpg.setGlobalConfig(gc);


        // 、数据源配置

        DataSourceConfig dsc = new DataSourceConfig();

        dsc.setUrl("jdbc:mysql://yun.888808888.top:3306/db_pipe?serverTimezone=GMT%2B8");

        dsc.setDriverName("com.mysql.cj.jdbc.Driver");

        dsc.setUsername("root");

        dsc.setPassword("jwdlh123");

        dsc.setDbType(DbType.MYSQL);

        mpg.setDataSource(dsc);


        // 、包配置

        PackageConfig pc = new PackageConfig();

        pc.setModuleName("servicepipe"); //模块名

        pc.setParent("com.pipe");

        pc.setController("controller");

        pc.setEntity("entity");

        pc.setService("service");

        pc.setMapper("mapper");

        mpg.setPackageInfo(pc);


        // 、策略配置

        StrategyConfig strategy = new StrategyConfig();
        //表名
        strategy.setInclude("pipe_debt","pipe_log","pipe_product","pipe_repayment","pipe_sell","pipe_product_sell");

        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略

        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀


        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略

        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作


        strategy.setRestControllerStyle(true); //restful api风格控制器

        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符


        mpg.setStrategy(strategy);


        // 、执行

        mpg.execute();
    }
}
